package com.lmc.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lmc.R;
import com.lmc.entity.HealthData;
import com.lmc.health.rate.DataFragment;

import java.util.List;

public class DataListAdapter extends BaseAdapter {

    Context context;
    List<HealthData> list;

    public DataListAdapter(Context context, List<HealthData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public HealthData getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.data_list_item, null);
            holder = new ViewHolder();
            holder.iv = convertView.findViewById(R.id.iv);
            holder.text = convertView.findViewById(R.id.text);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final HealthData data = getItem(position);
        holder.text.setText(data.getDay());

        if (data.getTag() == 0) {
            holder.text.setTextColor(Color.parseColor("#FF999999"));
            holder.text.setBackgroundResource(0);
            holder.iv.setVisibility(View.INVISIBLE);
        } else if (data.getTag() == 1) {
            holder.text.setTextColor(Color.parseColor("#FF333333"));
            holder.text.setBackgroundResource(0);
            holder.iv.setVisibility(View.INVISIBLE);
        } else if (data.getTag() == 2) {
            holder.text.setTextColor(Color.parseColor("#FF333333"));
            holder.text.setBackgroundResource(0);
            holder.iv.setVisibility(View.VISIBLE);
        }

        //选择的样式单独处理
        if (data.getSelect() == 1) {
            holder.text.setTextColor(Color.parseColor("#ffffff"));
            holder.text.setBackgroundResource(R.drawable.lmc_y);
            holder.iv.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView iv;
        TextView text;
    }
}
